﻿using ImageK.Gui;
using System;
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using System.DirectoryServices;
using System.IO.Packaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ImageK.Java.Extensions;

namespace ImageK.Plugin
{
    /** This plugin implements the Plugins/Utilities/Monitor Events command.
        By implementing the IJEventListener, CommandListener, ImageListener
        and RoiListener interfaces, it is able to monitor foreground and background
        color changes, tool switches, Log window closings, command executions, image
        window openings, closings and updates, and ROI changes.
    */
    public class EventListener : PlugIn, IJEventListener, ImageListener, RoiListener, CommandListener {

	    public void run(String arg)
        {
            // IJ.addEventListener(this);
            // Executer.addCommandListener(this);
            ImagePlus.addImageListener(this);
            Roi.addRoiListener(this);
            IJ.log("EventListener started");
        }

        public void eventOccurred(int eventID)
        {
            switch (eventID)
            {
                case IJEventListener.FOREGROUND_COLOR_CHANGED:
                    String c = ToolBar.getForegroundColor().ToArgb().toHexString();
                    c = "#"+c.Substring(2);
                    IJ.log("Changed foreground color to "+c);
                    break;
                case IJEventListener.BACKGROUND_COLOR_CHANGED:
                    c = ToolBar.getBackgroundColor().ToArgb().toHexString();
                    c = "#"+c.Substring(2);
                    IJ.log("Changed background color to "+c);
                    break;
                case IJEventListener.TOOL_CHANGED:
                    string name = IJ.getToolName();
                    IJ.log("Switched to the "+name+(name.EndsWith("Tool") ? "" : " tool"));
                    break;
                case IJEventListener.COLOR_PICKER_CLOSED:
                    IJ.log("Color picker closed");
                    break;
                case IJEventListener.LOG_WINDOW_CLOSED:
                    //todo：
                    // IJ.removeEventListener(this);
                    // Executer.removeCommandListener(this);
                    ImagePlus.removeImageListener(this);
                    Roi.removeRoiListener(this);
                    IJ.ShowStatus("Log window closed; EventListener stopped");
                    break;
            }
        }

        // called when an image is opened
        public void imageOpened(ImagePlus imp)
        {
            IJ.log("Image opened: \""+imp.getTitle()+"\""+edt());
        }

        // Called when an image is closed
        public void imageClosed(ImagePlus imp)
        {
            IJ.log("Image closed: \""+imp.getTitle()+"\""+edt());
        }

        // Called when an image's pixel data is updated
        public void imageUpdated(ImagePlus imp)
        {
            IJ.log("Image updated: \""+imp.getTitle()+"\""+edt());
        }

        // Called when an image is saved
        public void imageSaved(ImagePlus imp)
        {
            IJ.log("Image saved: \""+imp.getTitle()+"\""+edt());
        }

        private String edt()
        {
            //todo:
            // return EventQueue.isDispatchThread() ? " (EDT)" : " (not EDT)";
            return " (not EDT)";
        }

        public String commandExecuting(String command)
        {
            IJ.log("Command executed: \""+command+"\" command");
            return command;
        }

        public void roiModified(ImagePlus img, int id)
        {
            String type = "UNKNOWN";
            switch (id)
            {
                case RoiListener.RoiEvents.CREATED: type="CREATED"; break;
                case RoiListener.RoiEvents.MOVED: type="MOVED"; break;
                case RoiListener.RoiEvents.MODIFIED: type="MODIFIED"; break;
                case RoiListener.RoiEvents.EXTENDED: type="EXTENDED"; break;
                case RoiListener.RoiEvents.COMPLETED: type="COMPLETED"; break;
                case RoiListener.RoiEvents.DELETED: type="DELETED"; break;
            }
            IJ.log("ROI modified: "+(img!=null ? img.getTitle() : "")+", "+type);
        }
    }

}
